package com.ying.dogying.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ying.dogying.entity.Response;
import com.ying.dogying.entity.dto.ImPostDto;
import com.ying.dogying.es.dto.EsPostDto;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/im-es")
public class EsController extends BaseController{

    @GetMapping(value = "/getEsSearch")
    public String getEsSearch(String keyword) {
        IPage<EsPostDto> pageData = esService.selectByParams(getPage(), keyword);
        request.setAttribute("pageData", pageData);
        request.setAttribute("keyword", keyword);
        return "es/search";
    }

    @GetMapping(value = "/initEsSearch")
    public @ResponseBody Response initEsSearch() {
        long total = 0;
        int size = 10000;
        Page page = new Page();
        page.setSize(size);

        for (int i = 1; i < 1000; i ++) {
            page.setCurrent(i);
            IPage<ImPostDto> dbData = imPostService.selectByParams(page, null, null, null, null, null);
            int num = esService.initEsSearch(dbData.getRecords());

            total += num;

            // 当一页查不出10000条的时候，说明是最后一页了
            if(dbData.getRecords().size() < size) {
                break;
            }
        }
        return new Response().success("ES索引初始化成功，共 " + total + " 条记录！");
    }

}